//
//  ShakeController.m
//  CoreAnimationDemo
//
//  Created by AY on 17/5/31.
//  Copyright © 2017年 Alex. All rights reserved.
//

#import "ShakeController.h"

#define Angle2Radian(angle) ((angle) / 180.0 * M_PI)

@interface ShakeController ()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
- (IBAction)start:(id)sender;
- (IBAction)stop:(id)sender;
@end

@implementation ShakeController

- (instancetype)init {
    self = [super init];
    if (self) {
        self = [self initWithNibName:NSStringFromClass([ShakeController class]) bundle:nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (IBAction)start:(id)sender {
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
    anim.keyPath = @"transform.rotation";
    anim.values = @[@(Angle2Radian(-5)),  @(Angle2Radian(5)), @(Angle2Radian(-5))];
    anim.duration = 0.1;
    // 动画的重复执行次数
    anim.repeatCount = MAXFLOAT;
    // 保持动画执行完毕后的状态
    anim.removedOnCompletion = NO;
    anim.fillMode = kCAFillModeForwards;

    [self.iconView.layer addAnimation:anim forKey:@"shake"];
}

- (IBAction)stop:(id)sender {
    [self.iconView.layer removeAnimationForKey:@"shake"];
}
@end
